package com.gx.smart.building.ui.common.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;

/**
 * @author: xiaosy
 * 时间: 2022/2/11
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public abstract class BasePagedListAdapter<E, VH extends RecyclerView.ViewHolder> extends PagedListAdapter<E, VH> {
    protected Context mContext;
    protected BaseAdapter.OnItemClickListener mItemClickListener;
    protected BaseAdapter.OnItemLongClickListener mOnItemLongClickListener;

    public BasePagedListAdapter(@NonNull DiffUtil.ItemCallback<E> diffCallback, Context context) {
        super(diffCallback);
        mContext = context;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(onBindLayout(), parent, false);
        return onCreateHolder(view);
    }

    //绑定布局文件
    protected abstract int onBindLayout();

    //创建一个holder
    protected abstract VH onCreateHolder(View view);

    //绑定数据
    protected abstract void onBindData(VH holder, E e, int position);

    @Override
    public void onBindViewHolder(@NonNull VH holder, @SuppressLint("RecyclerView") int position) {
        final E e = getItem(position);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(view -> mItemClickListener.onItemClick(e, position));
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(view -> mOnItemLongClickListener.onItemLongClick(e, position));
        }
        onBindData(holder, e, position);
    }

    public void setItemClickListener(BaseAdapter.OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setOnItemLongClickListener(BaseAdapter.OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener<E> {
        void onItemClick(E e, int position);
    }

    public interface OnItemLongClickListener<E> {
        boolean onItemLongClick(E e, int position);
    }
}
